//
//  QLChooseLocationVC.m
//  神约
//
//  Created by qlwl on 16/12/16.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLChooseLocationVC.h"


@interface QLChooseLocationVC ()<AMapLocationManagerDelegate,AMapSearchDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) AMapLocationManager * locationManagerr ; //
@property (nonatomic, strong) AMapSearchAPI * search  ; //

@property (nonatomic, assign) CLLocationCoordinate2D coor2D ; //
@property (nonatomic, strong) AMapReGeocodeSearchResponse * response ; //
@end

@implementation QLChooseLocationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.locationManagerr = [[AMapLocationManager alloc] init];
    [self.locationManagerr setDesiredAccuracy:kCLLocationAccuracyBest];
    //带逆地理（返回坐标和地址信息）
    self.locationManagerr.reGeocodeTimeout = 10;
    [self.locationManagerr setLocatingWithReGeocode:NO];
    [self startLocation];
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
}
- (void)startLocation
{
    self.locationManagerr.delegate = self;
    [self.locationManagerr startUpdatingLocation];
}
- (void)stopLocation
{
    self.locationManagerr.delegate = nil;
    [self.locationManagerr stopUpdatingLocation];
}

#pragma mark locationDelegate
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    [self stopLocation];
    NSLog(@"开始反地理编码");
    
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location = [AMapGeoPoint locationWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
    regeo.requireExtension =YES;
    regeo.radius = 2000;
    [self.search AMapReGoecodeSearch:regeo];
}
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if(response.regeocode != nil)
    {
        //通过AMapReGeocodeSearchResponse对象处理搜索结果
//        NSString *result = [NSString stringWithFormat:@"ReGeocode: %@", response.regeocode];
//        NSLog(@"%@ -- %@ -- %@",response.regeocode.formattedAddress,response.regeocode.addressComponent.streetNumber.street,response.regeocode.addressComponent.streetNumber.number );
//        NSLog(@"ReGeo: %@", result);
//        for (AMapPOI * poi in response.regeocode.pois) {
//            NSLog(@"%@---%@",poi.name,poi.address);
//        }
        self.response = response;
        [self.tableView reloadData];
    }
    
}


#pragma mark TabDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.response.regeocode.pois.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    AMapPOI * poi = self.response.regeocode.pois[indexPath.row];
    cell.textLabel.text = poi.name;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.text = poi.address;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = [UIColor darkGrayColor];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AMapPOI * poi = self.response.regeocode.pois[indexPath.row];
    if (self.selectLocationAddress) {
        self.selectLocationAddress(poi);
        [self.navigationController popViewControllerAnimated:YES];
    }
}





@end

